It’s a simple code for ‘Sorting a select box’ using jquery, coffeescript

Sorting a Select box using Jquery, Coffeescript


I tried to find coffeescript code for sorting a select box.
But I didn’t find it.

Cause I just wrote own sorting select box code using jquery and coffeescript.

Copy and Paste below sorting select box coffeescript code and use it Freely.:D

Jquery, Coffeescript를 이용한 Select box 정렬 함수 입니다.

Simple Select Sorting Code (CoffeeScript Version)



 


#--Simple Select Sorting Method. (S3M:D)--

#sortingSelectBox(selectTagId,sortBy,order)

#selectTagId <select id="selectTagId"></select>

#sortBy [0:Text, 1:Value] - default:0

#order[0:Ascending, 1:Descending] - default:0

#Examples

#sortingSelectBox("mySelectTagId",1,1) sortBy Value in Descending order

#sortingSelectBox("mySelectTagId",1) sortBy Value in Ascending order

#sortingSelectBox("mySelectTagId") sortBy Text in Ascending order

#

#by JoongSeob Vito Kim - http://dorajistyle.pe.kr

sortingSelectBox = (selectBoxId,sortBy,order) ->

  sortBy ?= 0

  order ?= 0

  sortVal = 0

  if order is 1 then sortVal = 2

  orderValLeft = -1 + sortVal

  orderValRight = 1 - sortVal

  selectBox = $("select#"+selectBoxId)

  options = $("select#"+selectBoxId+" option")

  selectedVal = selectBox.val()

  sortedOption = options.clone()

  options.empty().remove()

  switch sortBy

    when 0

      sortedOption.sort((left,right)->

        leftText = left.text.toLowerCase()

        rightText = right.text.toLowerCase()

        if leftText < rightText then return orderValLeft

        if leftText is rightText then return 0

        orderValRight

      )

    else

      sortedOption.sort((left,right)->

        leftVal = left.value

        rightVal = right.value

        if leftVal < rightVal then return orderValLeft

        if leftVal is rightVal then return 0

        orderValRight

      )

  selectBox.append(sortedOption)

  selectBox.val(selectedVal)

 



  

Simple Select Sorting Code (JavaScript Version)


 


//Simple Select Sorting Method. (S3M:D)

//sortingSelectBox(selectTagId,sortBy,order)

//selectTagId <select id="selectTagId"></select>

//sortBy [0:Text, 1:Value] - default:0

//order[0:Ascending, 1:Descending] - default:0

//Examples

//sortingSelectBox("mySelectTagId",1,1) sortBy Value in Descending order

//sortingSelectBox("mySelectTagId",1) sortBy Value in Ascending order

//sortingSelectBox("mySelectTagId") sortBy Text in Ascending order

//

//by JoongSeob Vito Kim - http://dorajistyle.pe.kr

sortingSelectBox = function(selectBoxId, sortBy, order) {

var options, orderValLeft, orderValRight, selectBox, selectedVal, sortVal, sortedOption;

if (sortBy != null) {

sortBy;

} else {

sortBy = 0;

};

if (order != null) {

order;

} else {

order = 0;

};

sortVal = 0;

if (order === 1) {

sortVal = 2;

}

orderValLeft = -1 + sortVal;

orderValRight = 1 - sortVal;

selectBox = $("select#" + selectBoxId);

options = $("select#" + selectBoxId + " option");

selectedVal = selectBox.val();

sortedOption = options.clone();

options.empty().remove();

switch (sortBy) {

case 0:

sortedOption.sort(function(left, right) {

var leftText, rightText;

leftText = left.text.toLowerCase();

rightText = right.text.toLowerCase();

if (leftText < rightText) {

return orderValLeft;

}

if (leftText === rightText) {

return 0;

}

return orderValRight;

});

break;

default:

sortedOption.sort(function(left, right) {

var leftVal, rightVal;

leftVal = left.value;

rightVal = right.value;

if (leftVal < rightVal) {

return orderValLeft;

}

if (leftVal === rightVal) {

return 0;

}

return orderValRight;

});

}

selectBox.append(sortedOption);

return selectBox.val(selectedVal);

};



 

  
by 月風